package com.online.controller;

import com.online.entity.*;
import com.online.service.FillQuestionService;
import com.online.service.JudgeQuestionService;
import com.online.service.MultiQuestionService;
import com.online.service.PaperManageService;
import com.online.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PaperManageController {
    @Autowired
    private PaperManageService paperManageService;
    //判断
    @Autowired
    private JudgeQuestionService judgeQuestionService;
    //选择
    @Autowired
    private MultiQuestionService multiQuestionService;
    //填空
    @Autowired
    private FillQuestionService fillQuestionService;

    @GetMapping("/papers")
    public ApiResult<PaperManage> findAll(){
        ApiResult res= ApiResultHandler.buildApiResult(200,"请求成功",paperManageService.findAll());
        return res;
    }
    @GetMapping("/papers/{paperId}")
    public Map<Integer, List<?>> findById(@PathVariable("paperId") Integer paperId){
        //1.选择题库
       List<MultiQuestion> multiQuestions= multiQuestionService.findByIdAndType(paperId);
       //2.填空题库
       List<FillQuestion> fillQuestions=fillQuestionService.findByIdAndType(paperId);
       //3.判断题库
        List<JudgeQuestion> judgeQuestions=judgeQuestionService.findByIdAndType(paperId);;
        Map<Integer,List<?>> map=new HashMap<>();
        map.put(1,multiQuestions);
        map.put(2,fillQuestions);
        map.put(3,judgeQuestions);
        return map;
    }
    @PostMapping("/paperManage")
    public ApiResult add(@RequestBody PaperManage paperManage) {
        int res = paperManageService.add(paperManage);
        if (res != 0) {
            return ApiResultHandler.buildApiResult(200,"添加成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"添加失败",res);
    }

}
